const URL = "http://110.41.43.118:3008";

const json = {
		get: function(url,data,call){
		let xhr = new XMLHttpRequest();
		
		xhr.onreadystatechange = function(){
			if(xhr.readyState == 4){
				if(xhr.status >= 200 && xhr.status < 300 || xhr.status == 304){
					call(JSON.parse(xhr.response));
				};
			};
		};
		xhr.open("get",url + "?" + json.pinj(data))
		
		xhr.send();
	   },
	   post: function(url,data,call){
		  let xhr = new XMLHttpRequest();
		   
		   xhr.onreadystatechange = function(){
		   	if(xhr.readyState == 4){
		   		if(xhr.status >= 200 && xhr.status < 300 || xhr.status == 304){
		   			call(JSON.parse(xhr.response));
		   		};
		   	};
		   };
		  xhr.open("post",url);
		  
		  xhr.setRequestHeader('Content-Type', 'Application/x-www-form-urlencoded');
		  
		  xhr.send(json.pinj(data));
	   },
	   all: function(obj){
		   let xhr = new XMLHttpRequest();
		   
		   xhr.onreadystatechange = function(){
		   	if(xhr.readyState == 4){
		   		if(xhr.status >= 200 && xhr.status < 300 || xhr.status == 304){
		   			obj.success(JSON.parse(xhr.response));
		   		};
		   	};
		   };
		   
		   if(obj.method == "get"){
		   		   xhr.open("get",obj.url + "?" + json.pinj(obj.data))
		   		   
		   		   xhr.send();
		   }else{
		   		   xhr.open("post",obj.url);
		   		   
		   		   xhr.setRequestHeader('Content-Type', 'Application/x-www-form-urlencoded');
		   		   
		   		   xhr.send(json.pinj(obj.data));
		   }
		   
	   },
	   
	   pinj: function(data){
		   let arr = [];
		   for(let k in data){
		   	arr.push(k +"=" +window.encodeURIComponent(data[k]));
		   }
		   
		   return arr.join("&")
		},
		
		getUrlData : function(num){
			let str = window.location.search.replace('?','');
			
			
			let arr = str.split('&');
			// console.log(str);
			let obj = {};
			
			
			for(let i = 0; i < arr.length; i++){
				//catId=0082 => [catId, 0082]
				let inArr = arr[i].split('=');
				// console.log(inArr)
				obj[inArr[0]] = inArr[1];
			};
			
			return obj[num]? window.decodeURIComponent(obj[num]) : null;
		}
		
	
};